module Explode.ScoreBoard;
private import Graphics.IGraphics;
private import Explode.Characters;
private import Explode.GameConfig;
private import Explode.Player;
private import Explode.Stage;
private import std.string;
private import std.format;


public class ScoreBoard :IDrawingEntry {
	protected GameStage _stage;
	protected GameParam _param;

	protected int _frameCnt = 0;
	protected int _goldCnt  = 0;
	protected int _lifeCnt  = 0;
	protected int _stageNum = 0;
	protected int _score	  = 0;

	public float x(){return 0;}
	public float y(){return 0;}
	public float z(){return 200;}
	
	public this( GameStage stage , GameParam param ) {
		_stage = stage;
		_param = param;
	} //
	
	public bool action(){
		return true;
	} //
	
	public bool drawingEntry( IGraphics g ) {
		return g.addDrawingEntry( this ) ;
	} //
/*
	private string format(...) {
		string _str = "";
		void _putc(dchar c)	{
			auto tmp = [c];
			_str ~= std.string.toUTF8( tmp );
		}
		doFormat(&_putc, _arguments, _argptr);
		return _str;
	} //
*/	
	public bool draw( IGraphics g ) {
		float clipX  = g.viewrect.x  , clipY  = g.viewrect.y , 
			  clipX2 = g.viewrect.x2 , clipY2 = g.viewrect.y2;
		const int W=16;
		const int H=16;

		Player p  = _stage.player;
		_frameCnt = _stage.frameCnt;
		_goldCnt  = _stage.goldCnt;
		_lifeCnt  = _param.life;
		_stageNum = _param.stageNum;
		_score	  = p.score + _param.score;
		g.setViewport( 0 , 0 , W * 40 , H * 30 ); 
		int bm  = g.blendMode;
		{
			Color c;
			c.color = 0xaa000033;
			g.drawFilledRect( 0 ,       0    , W * 40 , 18 , c );
			g.drawFilledRect( 0 ,  H * 29 -4 , W * 40 , 32 , c );

			g.blendMode = g.ALPHA_ADD;
			c.color = 0xccffff00;
			
			string top	  = format( "LIFE %d  STAGE %03d  MODE %s" 
									, _lifeCnt , _stageNum , _stage.cpu) ;
			string top2	  = format( "     %d        %03d       %s" 
									, _lifeCnt , _stageNum , _stage.cpu) ;
			string bottom  = format( "GOLD %03d   SCORE %06d   TIME %06d" , _goldCnt , _score , _frameCnt ) ;
			string bottom2 = format( "     %03d         %06d        %06d" , _goldCnt , _score , _frameCnt ) ;
			int tx = cast(int)(W * 40 - top.length * W   ) / 2;
			int bx = cast(int)(W * 40 - bottom.length *W ) / 2;

			g.drawText(  top   , tx  ,      2 , c , W , H );
			g.drawText( bottom , bx  , H * 29 -2 , c , W , H );

			g.blendMode = bm;
			c.color = ( _lifeCnt > 0 ) ? 0xccffffff : 0xccff0000;
			g.drawText(  top2  , tx  ,      2  , c , W , H );
			c.color = ( _goldCnt > 0 ) ? 0xccffffff : 0xcc00ffff; 
			g.drawText( bottom2, bx  , H * 29 -2 , c , W , H );
			if ( _param.bestTime > 0 ) {
				string btime = format( "                          BEST %06d" , _param.bestTime ) ;
				Color col = Color.create(( _frameCnt < _param.bestTime ) ? 0xffffff00 : 0xff888888 ) ;
				g.drawText( btime , bx  , H * 28 - 2 , col , W , H );
			}
		}
		g.setViewport( clipX , clipY , clipX2 , clipY2);
		return true;
	} //

} //


public class BackgroundText : IDrawingEntry {
	private GameStage _stage;
	private GameParam _param;
	public float x(){return 0;}
	public float y(){return 0;}
	public float z(){return -10;}
	public string text;
	public Color color;
	private int _frameCnt = 0;
	
	public this( GameStage stage , GameParam param ) {
		_stage = stage;
		_param = param;
		text = "Explode Runner";
		color = Color.create( 0xffff0000u );
	} //
	
	public bool action(){
		_frameCnt++;
		return true;
	} //
	
	public bool drawingEntry( IGraphics g ) {
		return g.addDrawingEntry( this ) ;
	} //

	public bool draw( IGraphics g ) {
		float clipX  = g.viewrect.x  , clipY  = g.viewrect.y , 
			  clipX2 = g.viewrect.x2 , clipY2 = g.viewrect.y2;
		const int W=32;
		const int H=48;
		text = _stage.title;
		g.setViewport( 0 , 0 , 640 , 480 ); 
		{
			int cx = ( 640 - text.length * W ) / 2;
			int cy = ( 480 - H				 ) / 3;
			g.drawText( text , cx , cy , color , W , H );
		}
		g.setViewport( clipX , clipY , clipX2 , clipY2);
		return true;
	} //

} //